// pages/my/mydata/index.js
const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    payList:{},
    imageURL:'',
    date:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let moeny = wx.getStorageSync('userInfo');
    this.setData({
      payList: moeny
    });
    
  },
  changeDate(e) {
    this.setData({ date: e.detail.value });
  },
  weight(e){
    //console.log(e)
    let payList = this.data.payList;
    payList.weight = e.detail.value;
    
  },
  

  move(){
    let that = this;
    if (this.data.payList.birthday === null){
      if(this.data.date == ''){
        wx.showToast({
          title: '请选择您的生日日期',
          icon: 'none'
        })
        return
      }
      App.ajax('mp/user/info', 'PUT', { weight: Number(that.data.payList.weight), head_img: that.data.payList.head_img, birthday: that.data.date }).then(res => {
        //console.log(res)
        wx.setStorage({
          key: "userInfo",
          data: res.data
        });
        wx.showToast({
          title: '修改成功',
        })
        wx.switchTab({
          url: '/pages/my/index',
        })
      }).catch(err => {
        console.log('报错了', err)
      })
      return
    }
    App.ajax('mp/user/info', 'PUT', { weight: Number(that.data.payList.weight),head_img:that.data.payList.head_img}).then(res => {
     // console.log(res)
      wx.setStorage({
        key: "userInfo",
        data: res.data
      });
      wx.showToast({
        title: '修改成功',
      })
      wx.switchTab({
        url: '/pages/my/index',
      })
    }).catch(err => {
      console.log('报错了', err)
    })
  },
  init(){
    App.UserInfo((res) => {
      this.setData({
        payList: res
      })
    });
  },
  //获取七牛token
  uptoken() {
    return new Promise((r, v) => {
      App.ajax('open/upload/token', 'GET', {}).then(res => {
        if (res && res.data) {
          r(res.data)
        }
      }).catch(err => {
        console.log('报错了', err)
        v()
      })
    })
  },
  //七牛上传
  handleUploadImage: function () {
    const qiniuUploader = require("../../../utils/qiniuUploader-min");
    let that = this;
    let {
      payList
    } = that.data
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        let tempFilePaths = res.tempFilePaths; 
        tempFilePaths.map((r, i) => {
          that.uptoken().then(function (res) { //异步变同步
            let uptoken = res.token
            let domain = res.domain
           // console.log(res, uptoken)
            qiniuUploader.upload(
              r, //上传的图片
              (res) => {  
                console.log(res)
                payList.head_img=res.imageURL;
                that.setData({
                  payList
                })
                
              }, (error) => { 
                console.log('error: ' + error);
              },
              { //华南地区
                region: 'SCN',
                uptoken: uptoken,
                uploadURL: 'https://up-z2.qbox.me',
                domain: domain+'/',
              })
          })
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})